package com.lanou3g.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanou3g.bean.Admin;
import com.lanou3g.bean.Fix;
import com.lanou3g.bean.Flats;
import com.lanou3g.service.AdminService;
import com.lanou3g.service.FixService;
import com.lanou3g.service.FlatsService;
import com.lanou3g.util.Result;

@Controller
@RequestMapping("/fix")
public class FixController {

	@Autowired
	private FixService fixService;

	@Autowired
	private FlatsService flatsService;

	@Autowired
	private AdminService adminService;

	// 进入维修列表页面
	@RequestMapping("/fixList")
	public String fixList() {
		return "fix/fixList";
	}

	// 获取维修列表信息
	@ResponseBody
	@RequestMapping("/getFixList")
	public Result getFixList(Integer page, Integer limit) {
		return fixService.getFixs(page, limit);
	}

	// 进入添加维修记录页面
	@RequestMapping("/addFix")
	public String addFix(HttpSession session) {
		// 获取所有公寓
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		// 获取维修人员列表
		List<Admin> fixPeoples = adminService.getFixAdmins();
		session.setAttribute("fixPeoples", fixPeoples);
		return "fix/addFix";
	}

	// 添加维修记录
	@ResponseBody
	@RequestMapping("/insertFix")
	public Result insertFix(Fix fix) {
		return fixService.insetFix(fix);
	}

	// 进入维修记录编辑页面
	@RequestMapping("/editFix/{id}")
	public String editFix(@PathVariable("id") Integer id, HttpSession session) {
		Fix fix = fixService.getFixById(id);
		session.setAttribute("fix", fix);
		// 获取所有公寓
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		// 获取维修人员列表
		List<Admin> fixPeoples = adminService.getFixAdmins();
		session.setAttribute("fixPeoples", fixPeoples);
		return "fix/editFix";
	}

	//更新维修记录
	@ResponseBody
	@RequestMapping("/updateFix")
	public Result updateFix(Fix fix) {
		return fixService.updateFix(fix);
	}
	
	//删除指定id的维修记录
	@ResponseBody
	@RequestMapping("/deleteFix/{id}")
	public Result deleteFix(@PathVariable("id")Integer id) {
		return fixService.deleteFix(id);
	}
}
